﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Ccit.Utils;
using System.Text;
using System.Collections.Generic;
using Newtonsoft.Json;
using Ccit.OA.MsSystemDoc;

namespace Ccit.OA.SystemDoc
{
    public partial class SystemDoc_class_tree :  Ccit.Utils.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this._hidSysDocId.Value = (null != Request.QueryString["SysDocId"]) ? Request.QueryString["SysDocId"].ToString() : "";
                this._hidClassType.Value = (null != Request.QueryString["classType"]) ? Request.QueryString["classType"].ToString() : "";
                this._hidNodeTab.Value = (null != Request.QueryString["NodeTab"]) ? Request.QueryString["NodeTab"].ToString() : "0";
                initializePage();
            }
        }
        /// <summary>
        /// 初始化页面
        /// </summary>
        private void initializePage()
        {
            createTree();
            trvDepAddress.ExpandLevel = 1;
        }
        private void createTree()
        {
            /* c创建根节点 */
            Microsoft.Web.UI.WebControls.TreeNode oRoot = new Microsoft.Web.UI.WebControls.TreeNode();
            ///调用创建根节点方法
            createRootNode(ref oRoot);
            ///添加第一层节点
            trvDepAddress.Nodes.Add(oRoot);
            ///添加节点信息
            createSubNodes(ref oRoot);
        }
        /// <summary>
        /// 创建根节点
        /// </summary>
        /// <param name="oRoot"></param>
        private void createRootNode(ref Microsoft.Web.UI.WebControls.TreeNode oRoot)
        {
            SystemDocBLL Bll = new SystemDocBLL();
            string NodeName = Bll.GetSysDocNode(_hidClassType.Value, _hidNodeTab.Value);
            oRoot.Text = NodeName;
            oRoot.ImageUrl = "../images/toolbar/Folder_open.gif";
            oRoot.NavigateUrl = "";
            oRoot.Target = "";
            oRoot.ID = "0_0";
            Bll = null;
        }
        /// <summary>
        /// 节点信息
        /// </summary>
        /// <param name="oRoot"></param>
        private void createSubNodes(ref Microsoft.Web.UI.WebControls.TreeNode oRoot)
        {
            SystemDocBLL Bll = new SystemDocBLL();
            string strAddrXML = Bll.GetSysDocNodes(_hidClassType.Value,_hidNodeTab.Value);   // get one stair ast type 
            Microsoft.Web.UI.WebControls.TreeNode oTreeNode;
            XElement XeAddr = XElement.Parse(strAddrXML);
            string SysDocID = "";
            string SysDocNAME = "";
            string hasChild = "";
           
            var eAddrArray = from eAddr in XeAddr.Elements("ADDR") select eAddr;

            foreach (var eAddr in eAddrArray)
            {
                SysDocID = eAddr.Element("SysDocID").Value;
                SysDocNAME = eAddr.Element("SysDocNAME").Value;
                hasChild = eAddr.Attribute("HASCHILD").Value; 
                oTreeNode = createTreeNode(SysDocNAME, SysDocID, hasChild);
                oRoot.Nodes.Add(oTreeNode);
            }
            XeAddr = null;
            Bll = null;
        }
        /// <summary>
        /// 创建节点信息
        /// </summary>
        /// <param name="addrName">节点名称</param>
        /// <param name="addrId">节点id</param>
        /// <param name="hasChild">子节点id</param>
        /// <param name="state">状态</param>
        /// <returns></returns>
        private Microsoft.Web.UI.WebControls.TreeNode createTreeNode(string SysDocNAME, string SysDocID, string hasChild)
        {
            Microsoft.Web.UI.WebControls.TreeNode oNode = new Microsoft.Web.UI.WebControls.TreeNode();

            oNode.Text = SysDocNAME;

            string sParam = "SysDocID=" + SysDocID + "&classType=" + this._hidClassType.Value + "&State=2&CreateNodeId=1";
            string encodeParam = Base64Lib.Encode(sParam);
            oNode.NavigateUrl = "./SystemDoc_class_edit.aspx?" + encodeParam;
            oNode.ImageUrl = "../images/toolbar/filelogo.gif";
            oNode.Target = "iFrmEdit";
            oNode.ID = SysDocID + "_" + "0";
            oNode.Type = "root";

            if (hasChild == "1")
            {
                Microsoft.Web.UI.WebControls.TreeNode oSubNode = new Microsoft.Web.UI.WebControls.TreeNode();
                oNode.Nodes.Add(oSubNode);
            }

            return oNode;
        }
        #region Ajax Methods
        /// <summary>
        /// 新增节点信息get all sub dep addresses
        /// </summary>
        /// <param name="addrId"></param>
        /// <param name="nodeIndex"></param>
        /// <returns></returns>
        [global::System.Web.Services.WebMethod]
        public static string GetSubAddrsByUpAddrId(string addrId, string nodeIndex, string expandSign, string levelStruct,string sClassType,string sNodeTab)
        {
            //StringBuilder sb = new StringBuilder("<RESULT>");
            //const string cnsOperType = "DYNLOADNODES";
            //string strExeResult = "";  // 0: no exist 1: existed

            SystemDocBLL BLL = new SystemDocBLL();
            List<MsSecondClass> lstSec = BLL.GetSysDocNodesXML(addrId, sClassType, sNodeTab);
            // strExeResult = strExeResult.Replace("<", "&lt;").Replace(">", "&gt;"); 

            //sb.Append("<NODEINDEX>").Append(nodeIndex).Append("</NODEINDEX>");
            //sb.Append("<OPERTYPE>").Append(cnsOperType).Append("</OPERTYPE>");

            //sb.Append("<ExpandSign>").Append(expandSign).Append("</ExpandSign>");
            //sb.Append("<LevelStruct>").Append(levelStruct).Append("</LevelStruct>");
            //sb.Append("<NodeId>").Append(addrId).Append("</NodeId>");

            //sb.Append("<EXERESULT><![CDATA[").Append(strExeResult).Append("]]></EXERESULT>");
            //sb.Append("</RESULT>");
            BLL = null;
            string strResult = "GetSubTypesByColumnId" + "_" + JavaScriptConvert.SerializeObject(lstSec) + "_" + nodeIndex + "_" + expandSign + "_" + levelStruct + "_" + addrId;
            return strResult;
        }
        [global::System.Web.Services.WebMethod]
        public static string DeleteSysDcoMess(string SysDocId, string nodeIndex)
        {
            StringBuilder sb = new StringBuilder("<RESULT>");
            const string cnsOperType = "ADDRDELETE";

            SystemDocBLL BLL = new SystemDocBLL();
            List<string> lstResult = BLL.DeleteSysDocMess(SysDocId);
            var newObj = new { resultSign = lstResult[0], message = lstResult[1] };
            string serExpr = JavaScriptConvert.SerializeObject(newObj);
            sb.Append("<NODEINDEX>").Append(nodeIndex).Append("</NODEINDEX>");
            sb.Append("<OPERTYPE>").Append(cnsOperType).Append("</OPERTYPE>");
            sb.Append("<EXERESULT><![CDATA[").Append(serExpr).Append("]]></EXERESULT>");
            sb.Append("</RESULT>");
            BLL = null;
            return sb.ToString();
        }
        [global::System.Web.Services.WebMethod]
        public static string GetNodeLevelStruct(string dtNodeId)
        {
            StringBuilder sb = new StringBuilder("<RESULT>");
            //const string cnsOperType = "GetNodeLevelStruct";
            SystemDocBLL BLL = new SystemDocBLL();
            string levelStruct = BLL.GetNodeLevelStruct(dtNodeId);
            //var oResult = new { resultIds = levelStruct };
            //string strResult = JavaScriptConvert.SerializeObject(oResult);
            //sb.Append("<OPERTYPE>").Append(cnsOperType).Append("</OPERTYPE>");
            //sb.Append("<EXERESULT><![CDATA[").Append(strResult).Append("]]></EXERESULT>");
            //sb.Append("</RESULT>");
            //BLL = null;
            //return sb.ToString();
            var oResult = new { resultIds = levelStruct };
            string strResult = "GetNodeLevelStruct" + "_" + JavaScriptConvert.SerializeObject(oResult);
            return strResult;
        }
        #endregion
    }
}
